package cn.lele.reggie.controller;


import cn.lele.reggie.common.BaseContext;
import cn.lele.reggie.common.R;
import cn.lele.reggie.dto.OrdersDto;
import cn.lele.reggie.entity.*;
import cn.lele.reggie.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody OrdersDto orders){
        return orderService.submitOrder(orders);
    }

    /**
     * 后台查询所有订单信息
     * @param page
     * @param pageSize
     * @param username
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String username
                                    , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime
                                    , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        log.info("开始时间----》"+beginTime+"结束时间"+endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.like(username!=null,Orders::getUserName,username)
                                .between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime)
                                .orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage,ordersLambdaQueryWrapper);
        return R.success(ordersPage);
    }

    /**
     * 用户查看自己的订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page, Integer pageSize){
        return orderService.pageOrders(page,pageSize);
    }

    /**
     * 修改用户订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("修改成功");
    }

    /**
     * 列出用户订单，未使用
     * @return
     */

    @GetMapping("/list")
    public R<List> list(){
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());
        List<Orders> list = orderService.list(ordersLambdaQueryWrapper);
        return R.success(list);
    }
}
